/**
 * Copyright 2012 Alfredo "Rainbowbreeze" Morresi
 * 
 * This file is part of Eureka! project.
 * 
 * Eureka! is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Eureka! is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with Eureka!. If not, see http://www.gnu.org/licenses/.
 */
package it.rainbowbreeze.eureka.logic;

import com.google.android.maps.GeoPoint;

import it.rainbowbreeze.eureka.common.AppEnv;
import it.rainbowbreeze.eureka.util.TestHelper;
import android.content.Context;
import android.graphics.Rect;
import android.test.AndroidTestCase;

/**
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class QualityDataPointManagerTest extends AndroidTestCase {
    // ------------------------------------------ Private Fields
    private QualityDataPointManager mQualityDataPointManager;

    // -------------------------------------------- Constructors

    // -------------------------------------- Setup and TearDown
    @Override
    protected void setUp() throws Exception {
        super.setUp();

        Context context = getContext();
        assertNotNull("Context is null", context);
        TestHelper.init(context);
        mQualityDataPointManager = AppEnv.i(getContext()).getQualityDataPointManager();
    }


    // --------------------------------------------------- Tests
    public void testFindTiles() {
        int zoom;
        
        zoom = 1;
        //every point is inside the first tile
        compareRect(0, 0, 0, 0,
                mQualityDataPointManager.findTiles(new GeoPoint(0, 0), 100000, 100000, zoom));
        compareRect(0, 0, 0, 0,
                mQualityDataPointManager.findTiles(new GeoPoint(-70000000, -150000000), 1000000, 1000000, zoom));
        compareRect(0, 0, 0, 0,
                mQualityDataPointManager.findTiles(new GeoPoint(70000000, 150000000), 1500000, 1500000, zoom));
        compareRect(0, 0, 0, 0,
                mQualityDataPointManager.findTiles(new GeoPoint(41103897, 12440928), 160000000, 675000000, zoom));
        

        //2x2 grid
        zoom = 2;
        compareRect(0, 0, 1, 1,
                mQualityDataPointManager.findTiles(new GeoPoint(0, 0), 100000, 100000, zoom));
        compareRect(0, 0, 0, 0,
                mQualityDataPointManager.findTiles(new GeoPoint(-70000000, -150000000), 1000000, 1000000, zoom));
        compareRect(1, 1, 1, 1,
                mQualityDataPointManager.findTiles(new GeoPoint(70000000, 150000000), 1500000, 1500000, zoom));

        //4x4 grid
        zoom = 4;
        compareRect(1, 1, 2, 2,
                mQualityDataPointManager.findTiles(new GeoPoint(0, 0), 100000, 100000, zoom));
        compareRect(0, 0, 0, 0,
                mQualityDataPointManager.findTiles(new GeoPoint(-70000000, -150000000), 1000000, 1000000, zoom));
        compareRect(3, 3, 3, 3,
                mQualityDataPointManager.findTiles(new GeoPoint(70000000, 150000000), 1500000, 1500000, zoom));
        compareRect(0, 0, 1, 1,
                mQualityDataPointManager.findTiles(new GeoPoint(-45000000, -90000000), 2000000, 2000000, zoom));
        compareRect(1, 0, 2, 3,
                mQualityDataPointManager.findTiles(new GeoPoint(0, -0), 4000000, 190000000, zoom));
    }
    
    // ----------------------------------------- Private Methods
    private void compareRect(int expectedTop, int expectedLeft, int expectedBottom, int expectedRight, Rect actual) {
        assertEquals("Wrong top", expectedTop, actual.top);
        assertEquals("Wrong left", expectedLeft, actual.left);
        assertEquals("Wrong bottom", expectedBottom, actual.bottom);
        assertEquals("Wrong right", expectedRight, actual.right);
    }

    
    // ----------------------------------------- Private Classes
}
